public class Leetcode43 {

    public String multiply(String num1, String num2) {
        if (num1.equals("0")||num2.equals("0")){
            return "0";
        }
        String res = "";
        for (int i = num2.length()-1; i >=0 ; i--) {
            int carry = 0;
            int currentNum = num2.charAt(i)-'0';
            // 中间结果
            StringBuffer temp = new StringBuffer();
            for (int j = 0; j < num2.length()-i-1; j++) {
                temp.append(0);
            }

            for (int j = num1.length()-1; j >=0||carry!=0 ; j--) {
                int currentNum2 = j>=0 ? num1.charAt(j)-'0' : 0;
                int tempNum = (currentNum*currentNum2+carry)%10;
                temp.append(tempNum);
                carry = (currentNum*currentNum2+carry)/10;
            }

            res = add(res,temp.reverse().toString());
        }
        return res;
    }

    public String add(String s1,String temp){
        int carry = 0;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = s1.length()-1,j = temp.length()-1; i >=0||j>=0||carry!=0; j--,i--) {
            int currentNum = i>=0 ? s1.charAt(i)-'0':0;
            int currentNum2 = j>=0 ? temp.charAt(j)-'0':0;
            int i1 = (currentNum + currentNum2 + carry) % 10;
            stringBuffer.append(i1);
            carry = (currentNum + currentNum2 + carry)/10;
        }
        return stringBuffer.reverse().toString();
    }

    public static void main(String[] args) {
        String a = "9133";
        String b ="0";
        Leetcode43 leetcode43 = new Leetcode43();
        leetcode43.multiply(a,b);
    }
}
